import axios from 'axios';
import store from '../store';

// 创建axios实例
const service = axios.create({
  baseURL: 'http://124.70.197.18:9200/cnzgc',
  timeout: 5000,
  headers: {
    'Content-Type': 'application/json',
    'AUTHORIZATION': localStorage.getItem('AUTHORIZATION') // 替换为你的认证token
  }
});


// 请求拦截器
service.interceptors.request.use(
  config => {
    const token = sessionStorage.getItem('token')
    if (token) {
      // 让每个请求携带token
      // config.headers['AUTHORIZATION'] = sessionStorage.getItem('token');
    }
    return config;
  },
  error => {
    // 请求错误处理
    console.error(error);
    return Promise.reject(error);
  }


);

// 响应拦截器
service.interceptors.response.use(
  response => {
    const res = response.data;
    if (res.code !== 200) {
      // 处理错误响应
      return Promise.reject(new Error(res.msg || 'Error'));
    } else {
      return res;
    }
  },
  error => {
    console.error('响应错误: ' + error);
    return Promise.reject(error);
  }
);


export default service;

